home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / tools / bison.lha / bison++-1.04 / derives.c < prev    next >
C/C++ Source or Header  |  1989-06-09  |  2KB  |  114 lines

  1. /* Match rules with nonterminals for bison,
  2.    Copyright (C) 1984, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* set_derives finds, for each variable (nonterminal), which rules can derive it.
  22.    It sets up the value of derives so that
  23.    derives[i - ntokens] points to a vector of rule numbers, terminated with a zero.  */
  24.  
  25. #include <stdio.h>
  26. #include "new.h"
  27. #include "types.h"
  28. #include "gram.h"
  29.  
  30.  
  31. short **derives;
  32.  
  33. void
  34. set_derives()
  35. {
  36.   register int i;
  37.   register int lhs;
  38.   register shorts *p;
  39.   register short *q;
  40.   register shorts **dset;
  41.   register shorts *delts;
  42.  
  43.   dset = NEW2(nvars, shorts *) - ntokens;
  44.   delts = NEW2(nrules + 1, shorts);
  45.  
  46.   p = delts;
  47.   for (i = nrules; i > 0; i--)
  48.     {
  49.       lhs = rlhs[i];
  50.       p->next = dset[lhs];
  51.       p->value = i;
  52.       dset[lhs] = p;
  53.       p++;
  54.     }
  55.  
  56.   derives = NEW2(nvars, short *) - ntokens;
  57.   q = NEW2(nvars + nrules, short);
  58.  
  59.   for (i = ntokens; i < nsyms; i++)
  60.     {
  61.       derives[i] = q;
  62.       p = dset[i];
  63.       while (p)
  64.     {
  65.       *q++ = p->value;
  66.       p = p->next;
  67.     }
  68.       *q++ = -1;
  69.     }
  70.  
  71. #ifdef    DEBUG
  72.   print_derives();
  73. #endif
  74.  
  75.   FREE(dset + ntokens);
  76.   FREE(delts);
  77. }
  78.  
  79. void
  80. free_derives()
  81. {
  82.   FREE(derives[ntokens]);
  83.   FREE(derives + ntokens);
  84. }
  85.  
  86.  
  87.  
  88. #ifdef    DEBUG
  89.  
  90. print_derives()
  91. {
  92.   register int i;
  93.   register short *sp;
  94.  
  95.   extern char **tags;
  96.  
  97.   printf("\n\n\nDERIVES\n\n");
  98.  
  99.   for (i = ntokens; i < nsyms; i++)
  100.     {
  101.       printf("%s derives", tags[i]);
  102.       for (sp = derives[i]; *sp > 0; sp++)
  103.     {
  104.       printf("  %d", *sp);
  105.     }
  106.       putchar('\n');
  107.     }
  108.  
  109.   putchar('\n');
  110. }
  111.  
  112. #endif
  113.  
  114.